<?php

/*
 * Copyright 2014 gg.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Wechat\Model;
require_once './Public/phpQuery.php';
/**
 * Description of Dataquery
 *
 * @author gg
 */
class DataqueryModel {
    private $urls;
    private $cookie,$key;
    public function __construct() {
        $this->cookie = TEMP_PATH."/cook/";
        $this->urls = array(
            'start'=>'http://ids.scuec.edu.cn/amserver/UI/Login?goto=http://my.scuec.edu.cn/index.portal',
            'login'=>'http://ids.scuec.edu.cn/amserver/UI/Login',
            'jw'=>'http://ssfw.scuec.edu.cn/ssfw/j_spring_ids_security_check',
            'exam'=>'http://ssfw.scuec.edu.cn/ssfw/xsks/kcxx.do',
            'index'=>'http://my.scuec.edu.cn/index.portal',
            'mark'=>'http://ssfw.scuec.edu.cn/ssfw/zhcx/cjxx.do',
            'class'=>'http://ssfw.scuec.edu.cn/ssfw/print/pkgl/kcbxx/4/',
            );
    }
    
    public function setKey($key){
        $this->key = "".$key;
    }
    
    

    public function login($user,$password){
//        echo $key."\n";
        $postData = "IDToken0=&IDToken1=".$user
        . "&IDToken2=".$password
        . "&IDButton=Submit&goto=aHR0cDovL215LnNjdWVjLmVkdS5jbi9pbmRleC5wb3J0YWw%3D&encoded=true&gx_charset=UTF-8";
        //获取session
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $this->urls['start']);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie.$this->key."1.coo");
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko');
        curl_exec($ch);
        curl_close($ch);
        //登陆
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $this->urls['login']);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie.$this->key."1.coo");
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie.$this->key."2.coo");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_REFERER, 'http://ids.scuec.edu.cn/amserver/UI/Login?goto=http://my.scuec.edu.cn/index.portal');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        $result =  curl_exec($ch);
        curl_close($ch);
//        echo $result;
        $tmp = mb_stristr($result, "span class=\"user\"");
//        echo "====>\n".var_dump($tmp);
        if ($tmp !=false){
            return TRUE;
        }else{
            return FALSE;
        }
        
    }
    
    public function jwCheck(){
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $this->urls['jw']);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie.$this->key."2.coo");
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie.$this->key."3.coo");
        //curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_REFERER, 'http://my.scuec.edu.cn/index.portal');
        //curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko');
//        echo curl_exec($ch);
        curl_exec($ch);
        curl_close($ch);
    }
    
    public function getExam(){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $this->urls['exam']);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie.$this->key."3.coo");
        //curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_REFERER, 'http://ssfw.scuec.edu.cn/ssfw/index.do');
        //curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko');
        $result = curl_exec($ch);
        curl_close($ch);
        \phpQuery::newDocumentString($result);
        $trs = pq("tr.t_con");
        $optStr = "";
        foreach ($trs as $tr) {
            $trText = pq($tr)->text();
            $arr = mb_split('\n', $trText);
            $num =  count($arr);
            if (($num==16)or($num==15)){
                $optStr.="课程:".  trim($arr[2])."\n".
                            "任课老师:".trim($arr[4])."\n".
                            "考试时间:".trim($arr[8])."\n".
                            "考试地点:".trim($arr[9])."\n".
                            "试卷类型:".trim($arr[10])."\n".
                            "考试状态:".trim($arr[12])."\n".
                            "------\n";
            }else{
                $optStr.="课程:".  trim($arr[2])."\n".
                        "考试地点:".trim($arr[4])."\n"."------\n";
            }
        }
        return mb_substr($optStr, 0, mb_strlen($optStr)-8);
    }
    
    
    public function getMark(){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $this->urls['mark']);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie.$this->key."3.coo");
        //curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_REFERER, 'http://ssfw.scuec.edu.cn/ssfw/index.do');
        //curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko');
        $result = curl_exec($ch);
        curl_close($ch);
        $optStr = "";
        \phpQuery::newDocumentString($result);
        $trs = pq('#form3')->parent()->find("tr.t_con");
        foreach ($trs as $tr) {
            $trText = pq($tr)->text();
            $arr = mb_split('\n', $trText);
//            print_r($arr);
            $num =  count($arr);
            if ($num == 33){
                $optStr.='学期:'.trim($arr[1])."\n".
                        '课程:'.trim($arr[3])."\n".
                        '学分:'.trim($arr[6])."\n".
                        '成绩:'.trim($arr[12]).trim($arr[11])."\n".
                        "------\n";
            }
        }
        return  mb_substr($optStr, 0, mb_strlen($optStr)-8);
    }
    
    
    public function getClassInfo(){
        $ch = curl_init();
        $xq = "";
        $m = date("m");
        $y = date("Y");
        if ($m < 3) {
            $xq = "".($y-1)."-$y-1";
        }elseif($m<8){
            $xq = "".($y-1)."-$y-2";
        }else{
            $xq = "".($y)."-".($y+1)."-1";
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $this->urls['class'].$xq.".do");
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie.$this->key."3.coo");
        //curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_REFERER, 'http://ssfw.scuec.edu.cn/ssfw/index.do');
        //curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko');
        $result = curl_exec($ch);
        curl_close($ch);
        $optStr = "";
        \phpQuery::newDocumentString($result);
        $table = pq("table.CourseFormTable")->html();
//        echo $this->key;
//        echo $table;
        if(($table!="")or($table!=null)){
            $cla = new \Wechat\Model\ClaModel();
            $cla->setCla($this->key,$table);
        }
        return $table;
//        return  mb_substr($optStr, 0, mb_strlen($optStr)-8);
    }
    
    
}
